package com.yx.yinshanjinniu.view;

import android.content.Context;

import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.UserInfoBean;
import com.yx.yinshanjinniu.utils.DataUtils;
import com.yx.yinshanjinniu.utils.RouteUtil;
import com.yx.yinshanjinniu.utils.ToastUtil;


public class CertificateModule {

    private static CertificateModule certModule = null;
    private static Context mContext;
    private CertificateDialog mCertDialog;
    private CertCardFailedDialog mCertCardFailedDialog;

    public static CertificateModule getInstance(Context context) {
        mContext = context;
        if (certModule == null) {
            synchronized (CertificateModule.class) {
                if (certModule == null) {
                    certModule = new CertificateModule();
                }
            }
        }
        return certModule;
    }

    public boolean isCertificate(String page) {
        UserInfoBean accountBean = DataUtils.getUserInfo(mContext);
        if (accountBean.auth_flag == 1) {
            if (accountBean.bank_flag == 1) {
                //银行实名成功
                return true;
            } else if (accountBean.bank_flag == 0) {
                //银行实名处理中
                ToastUtil.showToast(mContext, "银行开户申请中，请等待结果");
                return false;
            } else if (accountBean.bank_flag == 3 || accountBean.bank_flag == 2) {
                showCertCardFailedDialog(mContext);
                return false;
            } else if (accountBean.bank_flag == 4) {
                ToastUtil.showToast(mContext, "银行开户已冻结");
                return false;
            }
        } else {
            showCertificateDialog(mContext, page);
        }
        return false;
    }

    public void showCertificateDialog(final Context mContext, final String page) {
        mCertDialog = new CertificateDialog(mContext, new CertificateDialog.CertificateDialogListener() {
            @Override
            public void onConfirm() {
                RouteUtil.toCertificateIdCardActivity((BaseActivity) mContext);
                mCertDialog.dismiss();
            }

            @Override
            public void onCancel() {
                mCertDialog.dismiss();
            }
        });
        if (!mCertDialog.isShowing()) {
            mCertDialog.show();
        }
    }

    public void showCertCardFailedDialog(final Context mContext) {
        mCertCardFailedDialog = new CertCardFailedDialog(mContext, new CertCardFailedDialog.CertCardFailedDialogListener() {
            @Override
            public void onConfirm() {
                RouteUtil.toBindCardCertificateActivity((BaseActivity) mContext);
                mCertCardFailedDialog.dismiss();
            }

            @Override
            public void onCancel() {
                mCertCardFailedDialog.dismiss();
            }
        });
        if (!mCertCardFailedDialog.isShowing()) {
            mCertCardFailedDialog.show();
        }
    }

    public static String getNameHide(String name) {
        char[] chars = name.toCharArray();
        if (chars.length == 2) {
            name = name.replaceFirst(name.substring(1), "*");
        }
        if (chars.length > 2) {
            name = name.replaceFirst(name.substring(1, chars.length - 1), "*");
        }
        return name;
    }

    public static String getMobileHide(String mobile) {
        String hideMobile = mobile.substring(0, 3) + "****" + mobile.substring(7);
        return hideMobile;
    }

}
